package leetcode_cn;

import nowcoder.offer.Multiply;

/**
 * 字符串相乘
 *
 * @author briarbear
 * @blog https://briarbear.github.io
 * @date 2018-09-06 16:22
 */
public class Multipy {
    /**+6
     * https://leetcode.com/problems/multiply-strings/discuss/17605/Easiest-JAVA-Solution-with-Graph-Explanation
     */

    public String solution(String num1,String num2){
        int len1 = num1.length(),len2 = num2.length();
        int[] pos = new int[len1+len2];

        for (int i = len1-1; i  >= 0; i--) {
            for (int j = len2 -1;j >= 0;j--){
                int temp = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
                int p1 = i + j,p2 = i + j +1;
                int sum = temp + pos[p2];

                pos[p1] += sum / 10;
                pos[p2] = (sum) % 10;
            }
        }
        StringBuilder sb = new StringBuilder();
        for(int p : pos) if(!(sb.length() == 0 && p == 0)) sb.append(p);
        return sb.length() == 0 ? "0" : sb.toString();
    }


    public static void main(String[] args) {
        System.out.println(new Multipy().solution("78", "56"));
    }


}
